/*
Spotter Exercise Manager
Copyright 2011 Evan Nelson.

This file is part of Spotter Exercise Manager.

Spotter Exercise Manager is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Spotter Exercise Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Spotter Exercise Manager.  If not, see <http://www.gnu.org/licenses/>.

The author can be contacted by emailing ean5533@gmail.com
*/

#include <QtGui/QApplication>
#include <QFile>
#include <QMessageBox>
#include <QDir>

#include "mainwindow.h"
#include "login.h"
#include "database.h"


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString x = QDir::current().path();
    QDir::setCurrent(QCoreApplication::applicationDirPath());
     x = QDir::current().path();

    while(true) {
        //call login screen
        Login l;
        int personID = l.exec();

        //bail out if they cancelled
        if(personID < 1) return 0;

        //store user's ID
        Database::setCurPersonID(personID);

        //if this database didn't already exist, we'll need to pre-populate it
        QString sFile = Database::getDBFilePath();
        if(!QFile(sFile).exists()) {
            int response = QMessageBox("Create new user?", "The user ID you've entered does not exist. Would you like to create a new user?",
                                        QMessageBox::Question, QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton).exec();

            if(response != QMessageBox::Yes) continue;


            response = QMessageBox("Use sample data", "Would you like the database to be pre-populated with some sample weightlifting logs?",
                                                    QMessageBox::Question, QMessageBox::Yes, QMessageBox::No, QMessageBox::NoButton).exec();
            bool useSampleData = response == QMessageBox::Yes;
            Database::createDatabase(useSampleData);
        }

        break;
    }

    //call the main program
    MainWindow w;
    w.show();

    return a.exec();
}
